<template>
  <div class="minimal-banner" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave">
    <div class="banner-slider" :style="{ height: `${height}px` }">
      <div 
        class="banner-slides" 
        :style="{ transform: `translateX(-${currentIndex * 100}%)` }"
      >
        <div 
          v-for="(image, index) in images" 
          :key="index"
          class="banner-slide"
        >
          <img 
            :src="image" 
            :alt="`Banner ${index + 1}`" 
            class="banner-image"
          />
          <div class="banner-text">
            Banner {{ index + 1 }}
          </div>
        </div>
      </div>
      
      <!-- 轮播指示器 -->
      <div class="banner-indicators" v-if="images.length > 1">
        <span 
          v-for="(image, index) in images" 
          :key="index"
          class="indicator" 
          :class="{ active: index === currentIndex }"
          @click="goToSlide(index)"
        ></span>
      </div>
      
      <!-- 左右箭头 -->
      <button class="banner-arrow prev" @click="prevSlide" v-if="images.length > 1">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
        </svg>
      </button>
      <button class="banner-arrow next" @click="nextSlide" v-if="images.length > 1">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
        </svg>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'

// 轮播图图片 - 使用本地图片资源
import banner1 from '@/assets/img/1.png'
import banner2 from '@/assets/img/2.jpg'  
import banner3 from '@/assets/img/3.jpg'

const images = ref([
  banner1,
  banner2,
  banner3
])

// Props
const props = defineProps<{
  height?: number
  interval?: number
}>()

// 默认值
const height = props.height || 400
const interval = props.interval || 3000

// 状态
const currentIndex = ref(0)
let timer: number | null = null

// 轮播到下一张
const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % images.value.length
  console.log('Current slide:', currentIndex.value)
}

// 轮播到上一张
const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + images.value.length) % images.value.length
}

// 跳转到指定幻灯片
const goToSlide = (index: number) => {
  if (index >= 0 && index < images.value.length) {
    currentIndex.value = index
  }
}

// 开始自动轮播
const startAutoPlay = () => {
  stopAutoPlay()
  timer = window.setInterval(() => {
    nextSlide()
  }, interval)
  console.log('Auto play started')
}

// 停止自动轮播
const stopAutoPlay = () => {
  if (timer !== null) {
    clearInterval(timer)
    timer = null
    console.log('Auto play stopped')
  }
}

// 鼠标悬停时暂停，离开时恢复
const handleMouseEnter = () => {
  stopAutoPlay()
}

const handleMouseLeave = () => {
  startAutoPlay()
}

onMounted(() => {
  startAutoPlay()
})

onUnmounted(() => {
  stopAutoPlay()
})
</script>

<style scoped>
.minimal-banner {
  width: 100%;
  overflow: hidden;
  background: linear-gradient(45deg, #f0f0f0, #e0e0e0);
  min-height: 400px;
  position: relative;
}

.banner-slider {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.banner-slides {
  display: flex;
  width: 100%;
  transition: transform 0.5s ease;
  height: 100%;
}

.banner-slide {
  flex: 0 0 100%;
  height: 100%;
  position: relative;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 48px;
  font-weight: bold;
  color: white;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  border-radius: 10px;
}

.banner-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.indicator.active {
  background-color: white;
}

.banner-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.banner-arrow:hover {
  background-color: rgba(0, 0, 0, 0.5);
}

.banner-arrow.prev {
  left: 20px;
}

.banner-arrow.next {
  right: 20px;
}
</style>